﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.ComponentModel;
using System.Drawing;
using Djs.Tools.FileCommander.FileSystem;

namespace Djs.Tools.FileCommander.Components
{
    [DataGridViewColumnDesignTimeVisible(false)]
    public class FileListView : ListView
    {
        public FileListView()
        {
            this.DoubleBuffered = true;
            this.Font = new Font("Cambria", 9f, FontStyle.Bold);
            this.MultiSelect = true;
            this.HideSelection = false;
            this.FullRowSelect = true;
            this.BackColor = Color.DarkBlue;
            this.ForeColor = Color.Yellow;
            this.HeaderStyle = ColumnHeaderStyle.Clickable;
            this.BorderStyle = BorderStyle.Fixed3D;
            this.OwnerDraw = true;
            this.ShowItemToolTips = true;
            this.View = View.Details;

            this.SmallImageList = Components.FileIconStorage.ImageList;

            this.Columns.Add("File");
            this.Columns.Add("Ext");
            this.Columns.Add("Size");
            this.Columns.Add("Date");
            this.Columns[0].Width = 350;
            this.Columns[1].Width = 70;
            this.Columns[2].Width = 100;
            this.Columns[2].TextAlign = HorizontalAlignment.Right;
            this.Columns[3].Width = 120;
        }
        protected override void OnDrawColumnHeader(DrawListViewColumnHeaderEventArgs e)
        {
            e.DrawDefault = true;
            base.OnDrawColumnHeader(e);
        }
        protected override void OnDrawItem(DrawListViewItemEventArgs e)
        {
            e.DrawDefault = true;
            if (String.IsNullOrEmpty(e.Item.ImageKey) && e.Item is FileListItem)
            {
                FileItem item = (e.Item as FileListItem).FileItem;
                e.Item.ImageKey = FileIconStorage.GetImageKeyForFile(item.FullName);
            }
            base.OnDrawItem(e);
        }
        protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e)
        {
            e.DrawDefault = true;
            base.OnDrawSubItem(e);
        }
        protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
        {
            int wb = this.Width;
            base.SetBoundsCore(x, y, width, height, specified);
            int wa = this.Width;
            if (wb != wa) this.AccomodateColumnWidths();
        }

        protected void AccomodateColumnWidths()
        {
            if (this.Columns.Count == 0) return;

            int wd = this.Width - System.Windows.Forms.SystemInformation.VerticalScrollBarWidth - 6;
            int wc = 0;
            for (int c = 1; c < this.Columns.Count; c++)
                wc += this.Columns[c].Width;
            int w0 = wd - wc;
            this.Columns[0].Width = (w0 < 200 ? 200 : w0);
        }

        internal void FillFrom(FileSystem.FileGroup fileGroup)
        {
            this.Items.Clear();
            this._FileGroup = fileGroup;
            foreach (FileSystem.FileItem fileItem in fileGroup.ItemList)
                this.Items.Add(new FileListItem(fileItem));
        }
        private FileSystem.FileGroup _FileGroup;
    }
    public class FileListColumn : ColumnHeader
    { }
    public class FileListItem : ListViewItem
    {
        public FileListItem(FileSystem.FileItem fileItem)
        {
            this.FileItem = fileItem;
            switch (fileItem.FileType)
            {
                case FileSystem.FileType.UpDir:
                    this.Text = "[..]";
                    this.SubItems.Add("");
                    this.SubItems.Add("<DIR>");
                    this.SubItems.Add(fileItem.DateModify.ToString("dd.MM.yyyy HH:mm"));
                    this.ImageKey = FileIconStorage.GetImageKeyForSpec(FileIconStorage.SPEC_PREVIOUS);
                    break;
                case FileSystem.FileType.DownDir:
                    this.Text = "[" + fileItem.Name.ToUpper() + "]";
                    this.SubItems.Add("");
                    this.SubItems.Add("<DIR>");
                    this.SubItems.Add(fileItem.DateModify.ToString("dd.MM.yyyy HH:mm"));
                    this.ImageKey = FileIconStorage.GetImageKeyForSpec(FileIconStorage.SPEC_FOLDER);
                    break;
                case FileSystem.FileType.File:
                    this.Text = fileItem.Name;
                    this.SubItems.Add(fileItem.Extension);
                    this.SubItems.Add(fileItem.Length.ToString("### ### ### ###").Trim());
                    this.SubItems.Add(fileItem.DateModify.ToString("dd.MM.yyyy HH:mm"));
                    break;
            }
        }
        public FileSystem.FileItem FileItem { get; private set; }
    }
}
